(function () {
    var oGoodsCategory = document.getElementById("goods_category");
    var oImageListUl = document.getElementById("imagesList").getElementsByTagName("ul")[0];
    var oHotGoodsUl = document.getElementById("hot_goods_ul");
    var iPositionId = 0;

    //热门商品模板
    var sHotGoodsTemplate = '';

    /**
     * 处理商品分类数据
     */
    var doGoodsCategory = function (error,data) {
        var data=JSON.parse(data);
        var goods={},
            oGoodsCategoryTempt="";
        $.each(data, function (item) {
            goods[item.classID]=item.className;
            oGoodsCategoryTempt +="<li><a href='list.html?classID="+item.classID+"'><img src='./images/"+item.classID+".png' width='50px'><p>"+item.className+"</p></a></li>"
        });
        oGoodsCategory.innerHTML=oGoodsCategoryTempt;
        window.sessionStorage.setItem("goods",JSON.stringify(goods));
    };
    /**
     * 获取商品分类信息
     */
    var getGoodsCategory = function () {
        $.get(
            ucshop.getGoodsCategoryAddress(),
            null,
            doGoodsCategory
        );
    };

    /**
     * 处理热门商品信息
     * @param error
     * @param d
     */
    var doHotGoods = function (d) {
        ucshop.clearRange();
        if (d==0){
            alert("没有更多的数据了！");
            return;
        }
        try {
            oHotGoodsUl.innerHTML=null;
            $.each(d, function (item) {
                var discount= item.discount=='0'?1:(parseFloat(item.discount).toFixed(1)/10);
                item.discountPrice=parseFloat(item.price*discount).toFixed(2);
                var sLi = $.compile(sHotGoodsTemplate, item);
                oHotGoodsUl.innerHTML += sLi;
            });
        } catch (e) {
            console.log(e.errors);
        }
    };

    /**
     * 获取热门商品
     */
    var getHotGoods = function (pages) {
        $.loadTemplate('./template/list_goods.html', function (err, data) {
            if (err) {
                console.log(err);
                return;
            }
            sHotGoodsTemplate = data;

            $.jsonp(
                ucshop.getGoodsAddress(),
                {"format": "jsonp", "callback": "doHotGoods","pageCode":pages,'linenumber':12},
                'doHotGoods',
                 doHotGoods
            )

        });
    };

    /**
     * 获取广告数据
     * @param Error error 错误
     * @param string d 返回的数据
     */
    var doBanner = function (banner) {
            try {
                $.each(banner, function (item, index) {
                    if (index == 0) {
                        oImageListUl.innerHTML += '<li class="current"><a href=""><img src="images/default.jpg" alt=""></a></li>';
                    } else {
                        oImageListUl.innerHTML += '<li><a href=""><img src="images/default.jpg" alt=""></a></li>';
                    }
                });
                //图片预加载开始
                var imgs = oImageListUl.getElementsByTagName("img");
                $.each(banner, function (item, index) {
                    (function (index) {
                        $.preLoadImg(banner[index].url, function (data) {
                            imgs[index].src = data.src;
                        });
                    })(index);
                });
                //图片预加载结束
                loadCarousel();
            } catch (e) {
                console.log(e.errors);
            }
        };
    /**
     * 获取首页banner
     */
    var getBanner = function () {

        // $.get(
        //    'http://h6.duchengjiu.top/shop/api_ad.php',
        //     {"position_id": 1},
        //     doBanner
        // );
        var banner=[
            {url:'http://os000b8ha.bkt.clouddn.com/image/banner/banner1.png'},
            {url:'http://os000b8ha.bkt.clouddn.com/image/banner/banner4.jpg'},
            {url:'http://os000b8ha.bkt.clouddn.com/image/banner/banner5.jpg'},
            {url:'http://os000b8ha.bkt.clouddn.com/image/banner/banner6.jpg'},
            {url:'http://os000b8ha.bkt.clouddn.com/image/banner/banner2.png'},
            {url:'http://os000b8ha.bkt.clouddn.com/image/banner/banner3.jpg'}
        ];
        doBanner(banner);
    };

    var loadCarousel = function () {
        var imagesListLis = document.getElementById("imagesList")
            .getElementsByTagName("li");
        var circleListLis = document.getElementById("circles")
            .getElementsByTagName("li");
        var leftBtn = document.getElementById("carousel_leftBtn");
        var rightBtn = document.getElementById("carousel_rightBtn");
        var img_idx = 0; //合理值0、1、2、3、4、5、6、7
        rightBtn.onclick = function () {
            img_idx++;
            if (img_idx > 5) {
                img_idx = 0;
            }
            changePic();
        };
        leftBtn.onclick = function () {
            img_idx--;
            if (img_idx < 0) {
                img_idx = 7;
            }
            changePic();
        };
        for (var i = 0; i < circleListLis.length; i++) {
            (function (i) {
                circleListLis[i].onmouseover = function () {
                    img_idx = i;
                    changePic();
                };
            })(i);
        }

        function changePic() {
            for (var i = 0; i < imagesListLis.length; i++) {
                imagesListLis[i].className = "";
            }
            imagesListLis[img_idx].className = "current";
            for (var i = 0; i < circleListLis.length; i++) {
                circleListLis[i].className = "";
            }
            circleListLis[img_idx].className = "current";
        }
    };

    window.onload = function () {
        ucshop.checkLoginStatus();
        ucshop.toTop();
        ucshop.searchGoods();
        ucshop.slide_up();
        ucshop.progress();
        ucshop.pageShow(getHotGoods);
        //获取分类信息
        getGoodsCategory();
        //获取热门商品
        getHotGoods(0);
        //获取广告
        getBanner();
    }
})();